package com.cyandpos.pad.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by Administrator on 2020/2/7.
 * 日期类
 */

public class DateUtils {
    /**
     * 获取格式时间到秒
     */
    public static String getCurrentTime(){
        return DatePattern.mDateFormat4.format(Calendar.getInstance().getTime());
    }

    /**
     * 获取格式时间到天
     */
    public static String getCurrentDate(){
        return DatePattern.mDateFormat3.format(Calendar.getInstance().getTime());
    }

    /**
     * 获取格式当天时间yyyyMMddHHmmss
     * @return
     */
    public static String getCurrentTimeStr(){
        return DatePattern.mDateFormat6.format(Calendar.getInstance().getTime());
    }

    /**
     * 比较两个日期的大小，日期格式为yyyy-MM-dd
     * @param str1 the first date
     * @param str2 the second date
     * @return true <br/>false
     */
    public static boolean isDateOneBigger(String str1, String str2) {
        boolean isBigger = false;
        DateFormat sdf = DatePattern.mDateFormat3;
        Date dt1 = null;
        Date dt2 = null;
        try {
            dt1 = sdf.parse(str1);
            dt2 = sdf.parse(str2);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (dt1.getTime() > dt2.getTime()) {
            isBigger = true;
        } else if (dt1.getTime() < dt2.getTime()) {
            isBigger = false;
        }
        return isBigger;
    }


    /**
     *
     * @param date 把Date转化成Calendar
     * @return
     */
    public static Calendar dateToCalendar(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar;
    }

    /**
     * 把Calendar转化成Date
     *
     * @param calendar
     * @return
     */
    public static Date calendarToDate(Calendar calendar) {
        return calendar == null ? null : calendar.getTime();
    }

    /**
     * 时间日期转换
     * @param strDate 字符串yyyyMMddHHmmss
     * @return 字符串yyyy-MM-dd HH:mm:ss
     */
    public static String strToDateLong(String strDate) {
        Date date = new Date();
        try {
            date = new SimpleDateFormat("yyyyMMddHHmmss").parse(strDate);//先按照原格式转换为时间
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String str = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(date);//再将时间转换为对应格式字符串
        return str;
    }

}
